package lab2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import lab2.ClosedHashSet.CHSIterator;


public class Main {

	
	public static void main(String args[ ]) 
	{ 

        ClosedHashSet CHS;
        CHS = new ClosedHashSet(50);
        CHSIterator iterator=CHS.iterator();
        BufferedReader reader = null;
        

	    try {
	    	File input = new File("input.txt");
	    	File output = new File("output.txt");
	    	 if(!output.exists()){
	    		 output.createNewFile();
	         }
	    	try {
	    		reader = new BufferedReader(new InputStreamReader(new FileInputStream(input)));
	    	} catch (FileNotFoundException e1) {
	    		System.err.println("Input file is not found!");
	    		System.exit(0);
	    	}
	    	PrintWriter writer = new PrintWriter(output.getAbsoluteFile());
    		String line;
    		String s[];
	        while ((line = reader.readLine()) != null) {

				s=line.split(" ");
				switch(s[0]) {
				case "isEmpty": writer.println("isEmpty: " + CHS.isEmpty()); 
								break;
				case "remove":  try {
									int i=Integer.parseInt(s[1]);
									writer.println("remove: " + CHS.remove(i));
								} catch (NumberFormatException e) {  
							        System.err.println("Wrong string format!");   
								} 
								break;
				case "add": 	try {
									int i=Integer.parseInt(s[1]);
									writer.println("add: " + CHS.add(i));
								} catch (NumberFormatException e) {  
									System.err.println("Wrong string format!");   
								} catch (IllegalArgumentException e) {
									System.err.println("Argument can't be null!");
								}
								break;
				case "size": 	writer.println("size: " + CHS.size());
								break;
				case "contains":try {
									int i=Integer.parseInt(s[1]);
									writer.println("Contains " + i);
									writer.print(CHS.contains(i)); 
								} catch (NumberFormatException e) {  
									System.err.println("Wrong string format!");  	
								} 
								break;
				case "toString":writer.println("toString: " + CHS.toString());
								break;												
				case "clear":   writer.println("Set is clear"); 
								CHS.clear();
							    break;
				case "hasNext": writer.println("Has next: " + iterator.hasNext()); 
								break;
				case "next":	try {
								writer.println("next: " + iterator.next()); 
								} catch (NoSuchElementException e) {
									System.err.println(e);
								}
								break;
				default:		System.err.println("Unsupported command!"); 
				 				break;

	        }

	        }
           
		reader.close();
		writer.close();
	    } catch (IOException e) {
	    	System.err.println("File can't be closed!");
	    }
     }  
}